# https://contrast-ratio.com/
# https://contrast-grid.eightshapes.com/
def contrast(color1: int, color2: int):
    def int2RGB(c1: int):
        c2 = c1 // 16
        c3 = c2 // 16
        c4 = c3 // 16
        c5 = c4 // 16
        c6 = c5 // 16
        r = c6 % 16 * 16 + c5 % 16
        g = c4 % 16 * 16 + c3 % 16
        b = c2 % 16 * 16 + c1 % 16
        return [r, g, b]

    def luminanace(rgb_list):
        # rgb_list 格式: [r, g, b]
        for i in range(3):
            j = rgb_list[i] / 255
            if j <= 0.03928:
                rgb_list[i] = j / 12.92
            else:
                rgb_list[i] = ((j + 0.055) / 1.055) ** 2.4
        return rgb_list[0] * 0.2126 + rgb_list[1] * 0.7152 + rgb_list[2] * 0.0722

    rgb1 = int2RGB(color1)
    rgb2 = int2RGB(color2)
    lum1 = luminanace(rgb1)
    lum2 = luminanace(rgb2)
    if lum1 < lum2:
        lum1, lum2 = lum2, lum1
    return (lum1 + 0.05) / (lum2 + 0.05)
